'''
Created on 03/04/2010

@author: miguel
'''
import pygame
from Bug import Bug
from Instruction import Instruction
from vec2d import vec2d
from GameBoard import GameBoard
from Box import Box
import random

class ComputerBrain(object):
    #objectives_board = [[FREE for col in range(19)] for row in range(20)]

    def __init__(self, screen, game, field):
        self.screen = screen
        self.game = game
        self.field = field

        self.time_between_bugs = 200
        self.time_counter = 100

        self.com_sup_disc = None
        self.com_surp_dis = None
        self.user_sup_disc = None
        self.user_surp_dis = None
        self.bugs_num = None
        self.user_box_objective = None

    def update(self, com_sup_disc, com_surp_dis, user_sup_disc, user_surp_dis, bugs_num):
        self.com_sup_disc = com_sup_disc
        self.com_surp_dis = com_surp_dis
        self.user_sup_disc = user_sup_disc
        self.user_surp_dis = user_surp_dis
        self.bugs_num = bugs_num

        if self.time_counter > self.time_between_bugs and bugs_num < 2:
            self.game.computer.com_bugs.add(self.__create_new_bug())
            self.time_counter = 0
        self.time_counter = self.time_counter + 1

    def __create_new_bug(self):
        self.game.computer.bugs_number = self.game.computer.bugs_number + 1

        for user_box in self.game.player.user_boxes:
            if user_box.status != Box.OPEN:
                self.user_box_objective = user_box
                break

        if self.com_sup_disc == 0:
            instructions = self.__get_instructions_1(self.user_box_objective)
        elif self.com_sup_disc == 1:
            rand = random.randint(0, 10)
            if rand <= 6:
                instructions = self.__get_instructions_2(self.user_box_objective)
            else:
                instructions = self.__get_instructions_1(self.user_box_objective)
        elif self.com_sup_disc == 2:
            instructions = self.__get_instructions_2(self.user_box_objective)
            '''borrame'''
        elif self.com_sup_disc == 3:
            instructions = [Instruction(Instruction.EXPLODE, 1)]

        bug = Bug(self.screen, self.game, self.field, (270, 60), instructions, GameBoard.COM_BUG)
        return bug

    def __get_instructions_1(self, user_box_objective):
        instructions = []
        instructions.append(Instruction(Instruction.MOVE_DOWN, 3))
        bug_pos = vec2d(270, 150)
        box_pos = user_box_objective.position
        if box_pos.x < bug_pos.x:
            movements_x_needed = ((bug_pos.x - box_pos.x) / 30)
            x_side = Instruction.MOVE_LEFT
            #instructions.append(Instruction(Instruction.MOVE_LEFT, movements_x_needed))
        elif box_pos.x == bug_pos.x:
            pass
        else:
            movements_x_needed = ((box_pos.x - bug_pos.x) / 30)
            x_side = Instruction.MOVE_RIGHT
            #instructions.append(Instruction(Instruction.MOVE_RIGHT, movements_x_needed))

        if box_pos.y < bug_pos.y:
            movements_y_needed = ((bug_pos.y - box_pos.y) / 30)
            y_side = Instruction.MOVE_UP
            #instructions.append(Instruction(Instruction.MOVE_UP, movements_y_needed))
        elif box_pos.y == bug_pos.y:
            pass
        else:
            movements_y_needed = ((box_pos.y - bug_pos.y) / 30)
            y_side = Instruction.MOVE_DOWN
            #instructions.append(Instruction(Instruction.MOVE_DOWN, movements_y_needed))

        move_x_1 = random.randint(0, movements_x_needed)
        move_x_2 = movements_x_needed - move_x_1
        move_y_1 = random.randint(0, movements_y_needed)
        move_y_2 = movements_y_needed - move_y_1

        if move_x_1 != 0:
            instructions.append(Instruction(x_side, move_x_1))
        if move_y_1 != 0:
            instructions.append(Instruction(y_side, move_y_1))
        if move_x_2 != 0:
            instructions.append(Instruction(x_side, move_x_2))
        if move_y_2 != 0:
            instructions.append(Instruction(y_side, move_y_2))

        instructions.append(Instruction(Instruction.EXPLODE, 1))
        return instructions

    def __get_instructions_2(self, user_box_objective):
        instructions = []
        instructions.append(Instruction(Instruction.MOVE_DOWN, 3))
        bug_pos = vec2d(270, 150)
        box_pos = user_box_objective.position
        if box_pos.x < bug_pos.x:
            movements_x_needed = ((bug_pos.x - box_pos.x) / 30)
            instructions.append(Instruction(Instruction.MOVE_LEFT, movements_x_needed))
        elif box_pos.x == bug_pos.x:
            pass
        else:
            movements_x_needed = ((box_pos.x - bug_pos.x) / 30)
            instructions.append(Instruction(Instruction.MOVE_RIGHT, movements_x_needed))

        if box_pos.y < bug_pos.y:
            movements_y_needed = ((bug_pos.y - box_pos.y) / 30)
            instructions.append(Instruction(Instruction.MOVE_UP, movements_y_needed))
        elif box_pos.y == bug_pos.y:
            pass
        else:
            movements_y_needed = ((box_pos.y - bug_pos.y) / 30)
            instructions.append(Instruction(Instruction.MOVE_DOWN, movements_y_needed))


        instructions.append(Instruction(Instruction.EXPLODE, 1))
        return instructions
